package org.nicocube.airain.app.client;

import java.util.Map;

import org.nicocube.airain.app.client.ui.GameCharacterListPanel;
import org.nicocube.airain.app.client.ui.GameCharacterListPanelLauncher;
import org.nicocube.airain.app.client.ui.PlayerSubscriptionPanel;
import org.nicocube.airain.common.client.AirainMessenger;
import org.nicocube.airain.domain.client.dao.NoPlayerException;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class AirainApp implements EntryPoint {

	public interface I18nConstants extends Constants{
		@DefaultStringValue("Please wait while loading.")
		String waitLoading();

		@DefaultStringValue("Error while loading.")
		String errorLoading();

		@DefaultStringMapValue({"Male","Male","Female","Female"})
		Map<String, String> genders();
	}

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final AppGCServiceAsync gcService = GWT.create(AppGCService.class);
	private final PlayerRegistrationServiceAsync prService = GWT.create(PlayerRegistrationService.class);

	private AirainMessenger messenger;

	private I18nConstants constants;


	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		constants = GWT.create(I18nConstants.class);
		
		messenger = new AirainMessenger();
		messenger.show(constants.waitLoading(),true);

		gcService.getList(new GameCharacterListPanelLauncher(messenger,gcService) {

			@Override
			public void onSuccess(GameCharacterListPanel result) {
				RootPanel.get().add(result);
				messenger.hide();
			}

			@Override
			public void onFailure(Throwable caught) {
				try {
					throw caught;
				}
				catch (NoPlayerException e) {
					PlayerSubscriptionPanel subscriptionPanel = new PlayerSubscriptionPanel(e.getEmail(), prService);
					
					RootPanel.get().add(subscriptionPanel);
					
					messenger.hide();
				}
				catch (Throwable e) {
					messenger.show(constants.errorLoading());
				}
			}
		
		});
	}
}
